<?php
/**
 * 字符串处理函数库
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/11/8 0008
 * Time: 18:57
 * @author:xjw129xjt 531201650@qq.com
 */


/**
 * is_json    判断字符串是否是json字符串
 * @param $str
 * @return bool
 * @author:xjw129xjt 531201650@qq.com
 */
function is_json($str)
{
    json_decode($str);
    return (json_last_error() == JSON_ERROR_NONE);
}

function rsa_sign($data, $priKey)
{
    //转换为openssl密钥，必须是没有经过pkcs8转换的私钥
    $res = openssl_get_privatekey($priKey);
    //调用openssl内置签名方法，生成签名$sign
    openssl_sign($data, $sign, $res);
    //释放资源
    openssl_free_key($res);
    //base64编码
    $sign = base64_encode($sign);
    return $sign;
}


function hide_mobile($mobile)
{

    //1.字符串截取法
    //$rs = substr($mobile, 0, 3).'****'.substr($mobile, 7);
    //2.替换字符串的子串
    $rs = substr_replace($mobile, '****', 3, 4);
    //3.用正则
    // $rs = preg_replace('/(\d{3})\d{4}(\d{4})/', '$1****$2', $mobile);

    return $rs;
}


function hide_email($str)
{

    $email_array = explode("@", $str);
    $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($str, 0, 3); //邮箱前缀
    $count = 0;
    $str = preg_replace('/([\d\w+_-]{0,100})@/', '***@', $str, -1, $count);
    $rs = $prevfix . $str;
    return $rs;
}

